/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.product.service.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfo;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoService;
import com.thinkgem.jeesite.modules.product.dao.product.GenProductDao;
import com.thinkgem.jeesite.modules.product.entity.product.GenProduct;

/**
 * 商品管理Service
 * @author Egan
 * @version 2016-12-12
 */
@Service
@Transactional(readOnly = true)
public class GenProductService extends CrudService<GenProductDao, GenProduct> {

	@Autowired
	GenProductDao genProductDao;
	@Autowired 
	AccountInfoService accountInfoService;
	
	public GenProduct get(String id) {
		return super.get(id);
	}
	
	public List<GenProduct> findList(GenProduct genProduct) {
		return super.findList(genProduct);
	}
	
	public List<GenProduct> findListByCatalogId(String catalogid) {
		return genProductDao.findListByCatalogId(catalogid);
	}
	
	public List<GenProduct> findListByCatalogIdTrue(String catalogid) {
		return genProductDao.findListByCatalogIdTrue(catalogid);
	}
	
	public Page<GenProduct> findListByCatalogName(Page<GenProduct> page,GenProduct genProduct){
		genProduct.setPage(page);
		page.setList(genProductDao.findListByCatalogName(genProduct));
		return page;
	}
	
	
	public Page<GenProduct> findListByCatalogNameNotRecommend(Page<GenProduct> page,GenProduct genProduct) {
		genProduct.setPage(page);
		page.setList(genProductDao.findListByCatalogNameNotRecommend(genProduct));
		return page;
	}
	
	public Page<GenProduct> findRecomandParts(Page<GenProduct> page,GenProduct genProduct) {
		
		List<GenProduct> result = new ArrayList<GenProduct>();
		String associateStr = genProduct.getAssociateStr();
		if (!"".equals(associateStr)) {
			String[] associates = associateStr.split(",");
			
			for (String associate : associates) {
				
				if (!"".equals(associate)) {
					GenProduct genPro = new GenProduct();
					genPro.setId(associate);
					genPro = get(genPro);
					result.add(genPro);	
				}
			}
		}
		genProduct.setPage(page);
		page.setList(result);
		return page;
	}
	
	public Page<GenProduct> findListByIsNew(Page<GenProduct> page,GenProduct genProduct){
		genProduct.setPage(page);
		page.setList(genProductDao.findListByIsNew(genProduct));
		return page;
	}
	
	public Page<GenProduct> findPage(Page<GenProduct> page, GenProduct genProduct) {
		return super.findPage(page, genProduct);
	}
	
	@Transactional(readOnly = false)
	public void save(GenProduct genProduct) {
		if (genProduct.getDetail() != null) {
			genProduct.setDetail(StringEscapeUtils.unescapeHtml4(genProduct.getDetail().replaceAll("amp;", "")));
		}
		if (genProduct.getProducthtml() != null) {
			genProduct.setProducthtml(StringEscapeUtils.unescapeHtml4(genProduct.getProducthtml().replaceAll("amp;", "")));
		}
		
		if (genProduct.getIsNewRecord()){
			genProduct.preInsert();
			dao.insert(genProduct);
		}else{
			List<AccountInfo> infos = accountInfoService.findList(null);
			for (AccountInfo info:infos) {
				CacheUtils.remove(info.getId());
			}
			genProduct.preUpdate();
			dao.update(genProduct);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(GenProduct genProduct) {
		super.delete(genProduct);
	}
	@Transactional(readOnly = false)
	public void updateStatus(GenProduct genProduct) {
		genProductDao.updateStatus(genProduct);
	}
	
	@Transactional(readOnly = false)
	public void updateStock(GenProduct genProduct) {
		genProductDao.updateStock(genProduct);
	}

	public Long sumOutOfStock(){
		return genProductDao.sumOutOfStock();
	}
	
	public Page<GenProduct> findListByName(Page<GenProduct> page, GenProduct genProduct) {
		genProduct.setPage(page);
		page.setList(genProductDao.findListByName(genProduct));
		return page;
	}
	
	public Page<GenProduct> findPageStockOut(Page<GenProduct> page, GenProduct genProduct) {
		genProduct.setPage(page);
		page.setList(genProductDao.findListOutOfStock(genProduct));
		return page;
	}
}